package cn.iocoder.yudao.module.handguard.controller.admin.statistics;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.handguard.controller.admin.homeone.vo.HomeOneRespVO;
import cn.iocoder.yudao.module.handguard.controller.admin.statistics.vo.CountResVO;
import cn.iocoder.yudao.module.handguard.controller.admin.statistics.vo.PerformanceStatisticsResVO;
import cn.iocoder.yudao.module.handguard.controller.admin.statistics.vo.PlatformProfitStatisticsResVO;
import cn.iocoder.yudao.module.handguard.convert.homeone.HomeOneConvert;
import cn.iocoder.yudao.module.handguard.dal.dataobject.homeone.HomeOneDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.user.UserDO;
import cn.iocoder.yudao.module.handguard.dal.dataobject.userfollowup.UserFollowUpDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.user.MiniUserMapper;
import cn.iocoder.yudao.module.handguard.dal.mysql.userfollowup.UserFollowUpMapper;
import cn.iocoder.yudao.module.handguard.service.applicationdeductrecord.ApplicationDeductRecordService;
import cn.iocoder.yudao.module.handguard.service.applicationrecord.ApplicationRecordService;
import cn.iocoder.yudao.module.handguard.service.order.OrderService;
import cn.iocoder.yudao.module.handguard.service.userfollowup.UserFollowUpService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * @author: liuweijun
 * @since: 2023/9/12 15:58
 * @description:
 */
@Tag(name = "管理后台 - 统计相关")
@RestController
@RequestMapping("/hutai/statistics")
@Validated
public class StatisticsController {


    @Resource
    private OrderService orderService;

    @Resource
    private UserFollowUpService followUpService;


    @Resource
    private ApplicationDeductRecordService deductRecordService;

    @Resource
    private ApplicationRecordService applicationRecordService;

    @Resource
    private MiniUserMapper miniUserMapper;

    @GetMapping("/getCountData")
    @Operation(summary = "后台首页统计")
    public CommonResult<List<CountResVO>> getCountData() {
        List<CountResVO> countResVOList = new ArrayList<>();
        CountResVO c1 = new CountResVO();
        c1.setName("当天申请数（单）");
        Integer num =miniUserMapper.selectQuota();
        if (num==null){
            num = 0;
        }
        c1.setData(num);
        c1.setImage("el-icon-edit");
        CountResVO c2 = new CountResVO();
        c2.setName("成交订单统计（笔）");
        c2.setData(String.valueOf(miniUserMapper.countOrder()));
        c2.setImage("el-icon-edit");
        CountResVO c3 = new CountResVO();
        c3.setName("成交额度统计（元）");
        BigDecimal cjAmount =miniUserMapper.countCj();
        if (cjAmount==null){
            cjAmount = new BigDecimal(0.0);
        }
        c3.setData(cjAmount);
        c3.setImage("el-icon-edit");
        CountResVO c4 = new CountResVO();
        c4.setName("用户统计（人）");
        c4.setData(String.valueOf(miniUserMapper.selectCount(new LambdaQueryWrapperX<UserDO>().notIn(UserDO::getUserType,"0")).intValue()));
        c4.setImage("el-icon-edit");
        CountResVO c5 = new CountResVO();
        c5.setName("今日新增用户统计（人）");
        int c = miniUserMapper.countNowUser();
        c5.setData(String.valueOf(c));
        c5.setImage("el-icon-edit");
        CountResVO c6 = new CountResVO();
        c6.setName("成交用户统计（人）");
        c6.setData(String.valueOf(miniUserMapper.countOrder()));
        c6.setImage("el-icon-edit");

        countResVOList.add(c1);
        countResVOList.add(c2);
        countResVOList.add(c3);
        countResVOList.add(c4);
        countResVOList.add(c5);
        countResVOList.add(c6);

        return success(countResVOList);
    }


    @GetMapping("/getPerformanceStatistics")
    @Operation(summary = "后台首页-业绩统计")
    public CommonResult<List<String>> getPerformanceStatistics() {
        //当前年份
        int year = Calendar.getInstance().get(Calendar.YEAR);
        List<String> decimalList = orderService.getPerformanceStatistics(year);
        return success(decimalList);
    }


//    @GetMapping("/getAgentMyFollowUpUser")
//    @Operation(summary = "业务员数据统计-总待跟进客户")
//    @Parameter(name = "userId", description = "编号", required = true, example = "1")
//    public CommonResult<Integer> getAgentMyFollowUpUser(@RequestParam("userId") Long userId) {
//        int number = followUpService.getMyFollowUpUser(userId,2);
//        return success(number);
//    }

    @GetMapping("/getAgentWMyFollowUpUser")
    @Operation(summary = "业务员、权证员数据统计-总待跟进客户")
    @Parameter(name = "userId", description = "编号", required = true, example = "1")
    public CommonResult<Integer> getAgentWMyFollowUpUser(@RequestParam("userId") Long userId) {
        if (userId==0l){
            //获取当前用户id
            userId = getLoginUser().getId();
        }
        UserDO userDO = miniUserMapper.selectById(userId);
        int number = followUpService.getMyFollowUpUser(userId,Integer.valueOf(userDO.getUserType()));
        return success(number);
    }


    @GetMapping("/getAWUpUser")
    @Operation(summary = "业务员、权证-当月已跟进客户")
    @Parameter(name = "userId", description = "编号", required = true, example = "1")
    public CommonResult<Integer> getAWUpUser(@RequestParam("userId") Long userId) {
        if (userId==0l){
            //获取当前用户id
            userId = getLoginUser().getId();
        }
        int number = followUpService.getAWUpUser(userId);
        return success(number);
    }




    @GetMapping("/getMonthCjNumber")
    @Operation(summary = "业务员、权证员-当月已成交订单")
    @Parameter(name = "userId", description = "编号", required = true, example = "1")
    public CommonResult<Integer> getMonthCjNumber(@RequestParam("userId") Long userId) {
        if (userId==0l){
            //获取当前用户id
            userId = getLoginUser().getId();
        }
        //查询所有跟进订单，获取订单号查询已成交额度
        List<UserFollowUpDO> followUpDOList = followUpService.getMonthFollowUpList(userId);
        int number = 0;
        if (followUpDOList.size()>0){
            for (UserFollowUpDO f : followUpDOList){
                int b = orderService.getMonthCjNumber(f.getApplicationId());
                if (b>0){
                    number = number + b;
                }
            }
        }
        return success(number);
    }

    @GetMapping("/getMonthCjMoney")
    @Operation(summary = "业务员、权证员-当月已成交金额")
    @Parameter(name = "userId", description = "编号", required = true, example = "1")
    public CommonResult<BigDecimal> getMonthCjMoney(@RequestParam("userId") Long userId) {
        if (userId==0l){
            //获取当前用户id
            userId = getLoginUser().getId();
        }
        //查询所有跟进订单，获取订单号查询已成交额度
        List<UserFollowUpDO> followUpDOList = followUpService.getMonthFollowUpList(userId);
        BigDecimal sumMoney = new BigDecimal(0.00);
        if (followUpDOList.size()>0){
            for (UserFollowUpDO f : followUpDOList){
                BigDecimal b = orderService.getMonthCjMoney(f.getApplicationId());
                if (b!=null){
                    sumMoney = sumMoney.add(b);
                }
            }
        }
        return success(sumMoney);
    }



    @GetMapping("/getPlatformProfit")
    @Operation(summary = "平台利润（日、周、月、季度、半年、一年）")
    public CommonResult<List<PlatformProfitStatisticsResVO>> getPlatformProfit(@RequestParam("staType") Integer staType) {
        //查询所有跟进订单，获取订单号查询已成交额度
        List<PlatformProfitStatisticsResVO> resList = deductRecordService.getPlatformProfit(staType);
        return success(resList);
    }







}
